/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gmovil_jpa.controladores;

import gmovil_jpa.controladores.exceptions.NonexistentEntityException;
import gmovil_jpa.entidades.ValorAtributoPlan;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import gmovil_jpa.entidades.Plan;
import gmovil_jpa.entidades.Atributo;

/**
 *
 * @author castlerock
 */
public class ValorAtributoPlanJpaController {

    public ValorAtributoPlanJpaController() {
        emf = Persistence.createEntityManagerFactory("gmovil_jpaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ValorAtributoPlan valorAtributoPlan) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Plan plan = valorAtributoPlan.getPlan();
            if (plan != null) {
                plan = em.getReference(plan.getClass(), plan.getIdPlan());
                valorAtributoPlan.setPlan(plan);
            }
            Atributo atributo = valorAtributoPlan.getAtributo();
            if (atributo != null) {
                atributo = em.getReference(atributo.getClass(), atributo.getIdAtributo());
                valorAtributoPlan.setAtributo(atributo);
            }
            em.persist(valorAtributoPlan);
            if (plan != null) {
                plan.getValorAtributoPlanList().add(valorAtributoPlan);
                plan = em.merge(plan);
            }
            if (atributo != null) {
                atributo.getValorAtributoPlanList().add(valorAtributoPlan);
                atributo = em.merge(atributo);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ValorAtributoPlan valorAtributoPlan) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ValorAtributoPlan persistentValorAtributoPlan = em.find(ValorAtributoPlan.class, valorAtributoPlan.getIdValorAtributoPlan());
            Plan planOld = persistentValorAtributoPlan.getPlan();
            Plan planNew = valorAtributoPlan.getPlan();
            Atributo atributoOld = persistentValorAtributoPlan.getAtributo();
            Atributo atributoNew = valorAtributoPlan.getAtributo();
            if (planNew != null) {
                planNew = em.getReference(planNew.getClass(), planNew.getIdPlan());
                valorAtributoPlan.setPlan(planNew);
            }
            if (atributoNew != null) {
                atributoNew = em.getReference(atributoNew.getClass(), atributoNew.getIdAtributo());
                valorAtributoPlan.setAtributo(atributoNew);
            }
            valorAtributoPlan = em.merge(valorAtributoPlan);
            if (planOld != null && !planOld.equals(planNew)) {
                planOld.getValorAtributoPlanList().remove(valorAtributoPlan);
                planOld = em.merge(planOld);
            }
            if (planNew != null && !planNew.equals(planOld)) {
                planNew.getValorAtributoPlanList().add(valorAtributoPlan);
                planNew = em.merge(planNew);
            }
            if (atributoOld != null && !atributoOld.equals(atributoNew)) {
                atributoOld.getValorAtributoPlanList().remove(valorAtributoPlan);
                atributoOld = em.merge(atributoOld);
            }
            if (atributoNew != null && !atributoNew.equals(atributoOld)) {
                atributoNew.getValorAtributoPlanList().add(valorAtributoPlan);
                atributoNew = em.merge(atributoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = valorAtributoPlan.getIdValorAtributoPlan();
                if (findValorAtributoPlan(id) == null) {
                    throw new NonexistentEntityException("The valorAtributoPlan with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ValorAtributoPlan valorAtributoPlan;
            try {
                valorAtributoPlan = em.getReference(ValorAtributoPlan.class, id);
                valorAtributoPlan.getIdValorAtributoPlan();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The valorAtributoPlan with id " + id + " no longer exists.", enfe);
            }
            Plan plan = valorAtributoPlan.getPlan();
            if (plan != null) {
                plan.getValorAtributoPlanList().remove(valorAtributoPlan);
                plan = em.merge(plan);
            }
            Atributo atributo = valorAtributoPlan.getAtributo();
            if (atributo != null) {
                atributo.getValorAtributoPlanList().remove(valorAtributoPlan);
                atributo = em.merge(atributo);
            }
            em.remove(valorAtributoPlan);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ValorAtributoPlan> findValorAtributoPlanEntities() {
        return findValorAtributoPlanEntities(true, -1, -1);
    }

    public List<ValorAtributoPlan> findValorAtributoPlanEntities(int maxResults, int firstResult) {
        return findValorAtributoPlanEntities(false, maxResults, firstResult);
    }

    private List<ValorAtributoPlan> findValorAtributoPlanEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ValorAtributoPlan.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ValorAtributoPlan findValorAtributoPlan(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ValorAtributoPlan.class, id);
        } finally {
            em.close();
        }
    }

    public int getValorAtributoPlanCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ValorAtributoPlan> rt = cq.from(ValorAtributoPlan.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
